package cn.java79.quiz.service;

import cn.java79.quiz.dao.PractiseMapper;
import cn.java79.quiz.dao.QuestionsMapper;
import cn.java79.quiz.dao.UserMapper;
import cn.java79.quiz.entity.Practise;
import cn.java79.quiz.entity.Questions;
import cn.java79.quiz.entity.User;
import cn.java79.quiz.pojo.AjaxPage;
import cn.java79.quiz.util.LogUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author oldliu
 * @since 1.0
 */

@Transactional(readOnly = true)
@Service
//@Scope("singleton")
public class UserService {
    public static final int PAGESIZE = 10;
    @Autowired
    private UserMapper mapper;
    @Autowired
    private QuestionsMapper questionsMapper;
    @Autowired
    private PractiseMapper practiseMapper;

    public UserService(){
        System.out.println("UserSerivce 构造了。。。。。。。。。。。。。。。。。");
    }
    /**
     * @return
     */
    public AjaxPage<User> search(String loginname, String realname, String phone, int pageNo, int pageSize) {

        Page<User> page = PageHelper.startPage(pageNo, pageSize);

        mapper.search(loginname, realname, phone);

        return new AjaxPage<User>(page.subList(0, page.size()), page.getTotal(), page.getPageNum(), page.getPageSize(), page.getPages());

    }

    public AjaxPage<User> search(String loginname, String realname, String phone, int pageNo) {
        return this.search(loginname, realname, phone, pageNo, PAGESIZE);
    }

    public User get(Long id) {
        return mapper.get(id);

    }
    @Transactional(readOnly = false)
    public int save(User user) {
        try {
            int save = mapper.save(user);
            return save;
        } catch (Exception e) {
            LogUtil.handlerException(e);
        }
        return 0;
    }
    @Transactional(readOnly = false)
    public int del(long id) {

        int save = mapper.del(id);
        return save;

    }
    @Transactional(readOnly = false)
    public int update(User user) {
        try {
            int save = mapper.update(user);
            return save;
        } catch (Exception e) {
            LogUtil.handlerException(e);
        }
        return 0;
    }

    public User login(String loginname, String password) {

        return mapper.login(loginname, password);

    }

    public boolean checkMail(String email) {

        return mapper.checkEmail(email) > 0;

    }

    //返回 true，已经存在
    public boolean checkRepeat(String type, String value) {
        return mapper.checkRepeat(type, value) > 0;

    }

    /**
     * 根据邮件 重置密码
     *
     * @param mail
     * @param newpass
     * @return
     */
    public boolean resetPass(String mail, String newpass) {

        return mapper.resetPass(mail, newpass) > 0;

    }

    public boolean kofen(long id, int score) {

        return mapper.decrScore(id, score) > 0;

    }

    @Transactional(readOnly = false)
    public Questions showQuizAnswer(long userid, long qid) {

        Questions q = questionsMapper.getAnswer(qid);

        int r = mapper.decrScore(userid, q.getDegree());
        return q;

    }
    @Transactional(readOnly = false)
    public int  testAop(long userid, long qid) {
        Questions q = questionsMapper.getAnswer(qid);
        Practise p=new Practise();
        p.setUid(userid);
        p.setQid(qid);
        p.setResult(false);
        p.setAnswer("1");
        practiseMapper.add(p);
        int r = mapper.decrScore(userid, 2);
        if(r==0){
            throw new RuntimeException("用户号"+userid+"不存在！");
        }
        return r;

    }
}
